import {
  addDir,
  addFlowApply,
  addOutApply,
  addPreDownload,
  addShareApply,
  applyAudit,
  checkSensitive,
  copyFile,
  deleteFileOrDir,
  downloadBwdaOnlineEdit,
  editApply,
  getEditLinkPath,
  getPreviewLinkPath,
  getSupportUploadFileList,
  markFileMostUse,
  movePrivateFile,
  privateFilesDownload,
  queryBwdaEditAppState,
  queryDatadictList,
  queryDirTreeList,
  queryFileDir,
  queryGroupList,
  queryGroupListByUsercode,
  queryMyFiles,
  queryOptRight,
  querySensitive,
  queryShareDetail,
  querySms,
  queryUserListByOrgId,
  quickDownload,
  quickDownloadGroupFiles,
  scan,
  updateDir,
  updateFileMostUse,
  updateFileShare,
  upload,
  uploadFiles,
  uploadOutsideFiles,
  uploadFilesToGroup,
  queryScanTree,
} from '@/services/workspace';
import {queryUpload,} from '@/services/bnsConfig/upload';
import {queryUserList,} from '@/services/system/sysUser';
import {queryOrgList, queryTreeOrgList,} from '@/services/system/sysRole';
import { queryOrgAdminByUserCode,} from '@/services/system/org';


export default {
  namespace: 'privateFileModel',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 10,
      totalCount: 0,
    },
    //组织下的用户
    usersData: {
      dataList: [],
      pageNum: 0,
      pageSize: 10,
      totalCount: 0,
    },

    currentDir: {id: 0},//当前的目录

    //外发申请
    outApply: {},
    //流转申请
    flowApply: {},

    //目录树结构
    dirTreeList: [],


    //分组列表
    groupList: [],


    queryForm: {},
    /**
     * 文件的扫描结果
     */
    scanData:[],
    orgAdmin:[],
  },


  effects: {
    * queryMyFiles({payload, callback,}, {call, put}) {
      const response = yield call(queryMyFiles, payload);
      if (callback) callback(response);
      if (response.result == 'fail') {
        return;
      } else if (response.result == "ok") {
        yield put({
          type: 'save',
          payload: response,
        });
      }


    },

    * queryOutRightControl({payload, callback,}, {call, put}) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },
     * queryOrgAdmin({payload, callback,}, {call, put}) {
      const response = yield call(queryOrgAdminByUserCode, payload);
       if(response&&response.result=='ok'){
         yield put({
           type: 'adminOrg',
           payload: response,
         });
       }

      if (callback) callback(response);
    },
    * queryOutWaterControl({payload, callback,}, {call, put}) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },
  * queryHideFunctions({payload, callback,}, {call, put}) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },


    * queryOrgTree({payload, callback,}, {call, put}) {
      const response = yield call(queryTreeOrgList, payload);
      if (response.result == 'fail') {
        return;
      }
      if (callback) callback(response);
    },
    * queryScanTree({payload, callback,}, {call, put}) {
      const response = yield call(queryScanTree, payload);

      if (response.result == 'ok') {
        yield put({
          type: 'saveScanData',
          payload: response,
        });
      }
    },
    * queryScanTreeData({payload, callback,}, {call}) {
      const response = yield call(queryScanTree, payload);
      if (response.result == 'ok') {
        if (callback) callback(response.data);
      }
      
    },
    * getSupportUploadFileList({payload, callback,}, {call, put}) {
      const response = yield call(getSupportUploadFileList, payload);
      if (response.result == 'fail') {
        return;
      }
      if (callback) callback(response);
    },
    /**
     * 查询上传的配置
     * @param payload
     * @param callback
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */* queryUpload({payload, callback,}, {call, put}) {
      const response = yield call(queryUpload, payload);
      if (response.result == 'fail') {
        return;
      }
      if (callback) callback(response);
    },

    * queryOptRight({payload, callback,}, {call, put}) {
      const response = yield call(queryOptRight, payload);
      if (callback) callback(response);
    },

    * queryShareDetail({payload, callback,}, {call, put}) {
      const response = yield call(queryShareDetail, payload);
      if (callback) callback(response);
    },

    * queryUserList({payload, callback,}, {call, put}) {
      const response = yield call(queryUserList, payload);
      if (response.result == 'fail') {
        return;
      }
      yield put({
        type: 'saveUserList',
        payload: response,
      });
      // if (callback) callback(response);
    },
    * queryGroupList({payload, callback,}, {call, put}) {
      const response = yield call(queryGroupList, payload);
      if (response.result == 'fail') {
        return;
      }
      yield put({
        type: 'saveGroupList',
        payload: response,
      });
      // if (callback) callback(response);
    },

    * queryGroupListByUsercode({payload, callback,}, {call, put}) {
      const response = yield call(queryGroupListByUsercode, payload);
      if (response.result == 'fail') {
        return;
      }
      yield put({
        type: 'saveGroupListByUsercode',
        payload: response,
      });
      // if (callback) callback(response);
    },
    * queryFileDir({payload, callback}, {call, put}) {
      const response = yield call(queryFileDir, payload);
      if (callback) callback(response);
      if (response.result == "fail") {
        return;
      } else if (response.result == "ok") {
        yield put({
          type: 'saveFileDir',
          payload: response,
        });
      }

    },

    // 流转
    * addFlowApply({payload, callback}, {call, put}) {
      const response = yield call(addFlowApply, payload);
      if (callback) callback(response);
    },
    // 个人文件下载申请
    * privateFilesDownload({payload, callback}, {call, put}) {
      const response = yield call(privateFilesDownload, payload);
      if (callback) callback(response);
    },
// 编辑申请
    * editApply({payload, callback}, {call, put}) {
      const response = yield call(editApply, payload);
      if (callback) callback(response);
    },

    // 外发
    * out({payload, callback}, {call, put}) {
      const response = yield call(addOutApply, payload);
      if (callback) callback(response);
    },

    * deleteFileOrDir({payload, callback}, {call, put}) {
      const response = yield call(deleteFileOrDir, payload);
      if (callback) callback(response);
    },


    //更新常用
    * updateFileMostUse({payload, callback}, {call, put}) {
      const response = yield call(updateFileMostUse, payload);
      if (callback) callback(response);
    },

    //更新共享
    * updateFileShare({payload, callback}, {call, put}) {
      const response = yield call(updateFileShare, payload);
      if (callback) callback(response);
    },
    //预下载
    * addPreDownload({payload, callback}, {call, put}) {
      const response = yield call(addPreDownload, payload);
      if (callback) callback(response);
    },
    * downloadBwdaOnlineEdit({payload, callback}, {call, put}) {
      const response = yield call(downloadBwdaOnlineEdit, payload);
      if (callback) callback(response);
    },

    //下载
    * quickDownload({payload, callback}, {call, put}) {
      const response = yield call(quickDownload, payload);
      if (callback) callback(response);
    },
    //下载公共文件
    * quickDownloadGroupFiles({payload, callback}, {call, put}) {
      const response = yield call(quickDownloadGroupFiles, payload);
      if (callback) callback(response);
    },
    //添加文件夹
    * addDir({payload, callback}, {call, put}) {
      const response = yield call(addDir, payload);
      if (callback) callback(response);
    },
    //重命名文件夹
    * updateDir({payload, callback}, {call, put}) {
      const response = yield call(updateDir, payload);
      if (callback) callback(response);
    },
    /**
     * 获取编辑地址
     * @param payload
     * @param callback
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */* getEditLinkPath({payload, callback}, {call, put}) {
      const response = yield call(getEditLinkPath, payload);
      if (callback) callback(response);
    },
    * getPreviewLinkPath({payload, callback}, {call, put}) {
      const response = yield call(getPreviewLinkPath, payload);
      if (callback) callback(response);
    },

    * uploadFiles({payload, callback}, {call, put}) {
      const response = yield call(uploadFiles, payload);
      if (callback) callback(response);
    },
    * uploadFilesToGroup({payload, callback}, {call, put}) {
      const response = yield call(uploadFilesToGroup, payload);
      if (callback) callback(response);
    },
    * uploadOutsideFiles({payload, callback}, {call, put}) {
      const response = yield call(uploadOutsideFiles, payload);
      if (callback) callback(response);
    },
    //查询目录组织树
    * queryDirTreeList({payload, callback}, {call, put}) {
      const response = yield call(queryDirTreeList, payload);
      if (response.result == 'ok') {
        yield put({
          type: 'saveDirTreeList',
          payload: response,
        });
      }

    },
    //查询某人的自定义组
    * queryGroupListByUsercode({payload, callback}, {call, put}) {
      const response = yield call(queryGroupListByUsercode, payload);
      if (response.result == 'ok') {
        yield put({
          type: 'saveGroupListByUsercode',
          payload: response,
        });
      }

    },
    //移动文件
    * moveFile({payload, callback}, {call, put}) {
      const response = yield call(movePrivateFile, payload);
      if (callback) callback(response);
    },
    //复制文件
    * copyFile({payload, callback}, {call, put}) {
      const response = yield call(copyFile, payload);
      if (callback) callback(response);
    },


    * queryBwdaEditAppState({payload, callback}, {call, put}) {
      const response = yield call(queryBwdaEditAppState, payload);
      if (callback) callback(response);
    },

    * queryScan({payload, callback}, {call, put}) {
      const response = yield call(scan, payload);
      if (callback) callback(response);
    },
    * sensitive({payload, callback}, {call, put}) {
      const response = yield call(querySensitive, payload);
      if (callback) callback(response);
    },
    * sms({payload, callback}, {call, put}) {
      const response = yield call(querySms, payload);
      if (callback) callback(response);
    },
    * checkSensi({payload, callback}, {call, put}) {
      const response = yield call(checkSensitive, payload);
      if (callback) callback(response);
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveFileDir(state, action) {
      return {
        ...state,
        currentDir: action.payload.data,
      };
    },
    saveDirTreeList(state, action) {
      return {
        ...state,
        dirTreeList: action.payload.data,
      };
    },
    saveOrgTree(state, action) {
      return {
        ...state,
        orgTree: action.payload.data,
      };
    },
    saveGroupListByUsercode(state, action) {
      return {
        ...state,
        groupList: action.payload.data.dataList,
      };
    },
    saveUserList(state, action) {
      return {
        ...state,
        usersData: action.payload.data,
      };
    },
    /**
     * 文件扫描结果
     * @param state
     * @param action
     * @returns {{usersData: *}}
     */
    saveScanData(state, action) {
      return {
        ...state,
        scanData: action.payload.data,
      };
    },
    adminOrg(state, action) {
      return {
        ...state,
        orgAdmin: action.payload.data,
      };
    },


  },
};
